<template>
  <div>

    <div class="mg-10">
      <el-input style="width: 200px" suffix-icon="el-icon-search" placeholder="请输入角色名" v-model="name"/>
      <el-input class="ml-5" style="width: 200px" suffix-icon="el-icon-message" placeholder="请输入角色描述"
                v-model="description"/>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button class="ml-5" type="warning" @click="refresh">重置</el-button>
    </div>
    <div class="mg-10">
      <el-button type="primary" icon="el-icon-circle-plus-outline" @click="dialogFormVisible = true">新增
      </el-button>
      <el-popconfirm
          style="margin: 0px 10px"
          confirm-button-text='确认'
          cancel-button-text='取消'
          icon="el-icon-info"
          icon-color="red"
          title="确定要批量删除吗？"
          @confirm="deleteBatch"
      >
        <el-button type="danger" icon="el-icon-remove-outline" slot="reference">批量删除</el-button>
      </el-popconfirm>
      <el-upload action="http://localhost:8080/role/import" :show-file-list="false" accept="xlsx"
                 :on-success="handleExcelImportSuccess" style="display: inline-block">
        <el-button type="primary" icon="el-icon-bottom">导入</el-button>
      </el-upload>
      <el-button type="primary" icon="el-icon-top" @click="exp" style="margin-left: 10px">导出</el-button>
    </div>
    <el-table :data="tableData" border stripe @selection-change="handleSelectionChange"
              :header-cell-style="{background:'#eef1f6',color:'#606266'}">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="ID" width="60px">
      </el-table-column>
      <el-table-column prop="name" label="角色名" width="auto">
      </el-table-column>
      <el-table-column prop="description" label="角色描述" width="auto">
      </el-table-column>
      <el-table-column label="操作" width="280px" align="center">
        <template slot-scope="scope">
          <el-button
              type="info"
              size="mini" @click="handleAssign(scope.row.id)">分配菜单 <i class="el-icon-menu"/></el-button>
          <el-button
              size="mini"
              type="success"
              @click="handleEdit(scope.row)">编辑
          </el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确认'
              cancel-button-text='取消'
              icon="el-icon-info"
              icon-color="red"
              title="确定要删除吗？"
              @confirm="handleDelete(scope.row.id)"
          >
            <el-button
                size="mini"
                type="danger"
                slot="reference"
            >删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pd-10">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

    <el-dialog title="新增用户" :visible.sync="dialogFormVisible" width="30%"
               :before-close="dialogCancel">
      <el-form :model="roleForm" ref="roleForm" label-width="90px">
        <el-form-item label="角色名">
          <el-input v-model="roleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogCancel">取 消</el-button>
        <el-button type="primary" @click="saveRole">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="菜单分配" :visible.sync="menuDialogVisible" width="30%"
               :before-close="dialogAssignCancel">
      <el-tree
          :check-strictly="true"
          :data="menuData"
          :props="props"
          show-checkbox
          node-key="id"
          ref="tree"
          :default-expanded-keys="expands"
          :default-checked-keys="checks"
      >
         <span class="custom-tree-node" slot-scope="{ node, data }">
          <span><i :class="data.icon"/>{{ data.name }}</span>
         </span>
      </el-tree>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogAssignCancel">取 消</el-button>
        <el-button type="primary" @click="saveRoleMenu">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Role",
  data() {
    return {
      tableData: [],
      multipleSelection: [],
      roleForm: {
        name: '',
        description: '',
      },
      menuData: [],
      props: {
        label: 'name'
      },
      roleId:'',
      formLabelWidth: '120px',
      name: "",
      description: "",
      total: 0,
      pageNum: 1,
      pageSize: 5,
      dialogFormVisible: false,
      menuDialogVisible: false,
      expands:[],
      checks:[]
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      axios.get("/role/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name,
          description: this.description,
        }
      }).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
      })

    },
    saveRole() {
      axios.post("/role/save", this.roleForm).then(res => {
        if (res) {
          this.$message.success("角色添加成功！")
        } else {
          this.$message.error("角色添加失败！")
        }
        this.dialogFormVisible = false
        this.roleForm = {}
        this.load()
      })
    },
    dialogCancel() {
      this.roleForm = {}
      this.dialogFormVisible = false
    },
    handleEdit(row) {
      //防止提前出现变化
      this.roleForm = {...row}
      this.dialogFormVisible = true
    },
    handleAssign(roleId) {
      this.roleId=roleId
      this.menuDialogVisible = true
      //请求菜单数据
      axios.get("/menu").then(res => {
        this.menuData = res.data
        this.expands = this.menuData.map(v => v.id)
      })
      axios.get("/role/roleMenu/"+this.roleId).then(res => {
        this.checks = res.data.data
      })
    },
    saveRoleMenu(){
        axios.post("/role/roleMenu/"+this.roleId,this.$refs.tree.getCheckedKeys())
            .then(res => {
              this.$message.success("分配菜单成功！")
              this.menuDialogVisible=false
            })
    },
    dialogAssignCancel() {
      this.menuDialogVisible = false
    },
    handleDelete(row) {
      axios.delete("/role/del/" + row).then(res => {
        if (res) {
          this.$message.success("删除成功")
        } else {
          this.$message.error("删除失败")
        }
        this.load()
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    deleteBatch() {
      let ids = this.multipleSelection.map(v => v.id)
      axios.post("/role/del/batch", ids).then(res => {
        if (res) {
          this.$message.success("批量删除成功")
        } else {
          this.$message.error("批量删除失败")
        }
        this.load()
      })
    },
    refresh() {
      this.name = "",
          this.description = "",
          this.load()
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.load()
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    exp() {
      window.open("http://localhost:8080/role/export")
    },
    handleExcelImportSuccess() {
      this.$message.success("导入成功！")
      this.load()
    }
  }
}
</script>

<style scoped>

</style>